<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\School;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Controllers\AdminController;

use Illuminate\Http\Request;
use App\Models\School as SchoolModel;

class SchoolController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new SchoolModel(), function (Grid $grid) {
            $grid->model()->with(['school_anli']);
            $grid->school_anli('tongji1')->display(function($school_anli){
                $count = count($school_anli);
                return $count;
            });
            $grid->column('hidaas_id')->editable();
            $grid->column('school_name')->editable();
            $grid->column('school_logo')->editable();
            $grid->column('list_img')->editable();
            $grid->column('sort')->editable();
            $grid->column('rank')->editable();
            $grid->column('country_id')->editable();
            $grid->column('status')->editable();
            $grid->column('push')->editable();
            $grid->column('introduce')->editable();
    $grid->quickSearch('hidaas_id','school_name','school_logo','list_img','sort','rank','country_id','status','push','introduce');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new SchoolModel(), function (Show $show) {
            $show->id;
            $show->hidaas_id;
            $show->school_name;
            $show->school_logo;
            $show->list_img;
            $show->sort;
            $show->rank;
            $show->country_id;
            $show->status;
            $show->push;
            $show->introduce;
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new SchoolModel(), function (Form $form) {
            $form->display('id');
            $form->text('hidaas_id');
            $form->text('school_name');
            $form->text('school_logo');
            $form->text('list_img');
            $form->text('sort');
            $form->text('rank');
            $form->text('country_id');
            $form->text('status');
            $form->text('push');
            $form->text('introduce');
        });
    }

    /**
     * [listType 下拉关联查询]
     * @return [type] [description]
     */
    public function listType()
    {
        $request = new Request();
        $q = $request->get('q');
        return SchoolModel::where('type_name', 'like', "%$q%")->select('type_id as id', 'type_name as text')->get();
    }

}
